var main = new Vue({
	el: '#main',
	data: {
		titles: ["今日任务", "未完成任务", "已完成任务"],
		isActive: 1, //当前是那一项
		response: [],
		response2: [], //任务列表
		packup: false, //日期是否收起
		currentDay: 1, //日期类
		currentMonth: 1,
		currentYear: 1970,
		currentWeek: 1,
		days: [],
		arrDate: [], //未完成日期
		daysclick: new Date().toLocaleDateString(), //点击的那一天
		objanswer: JSON.parse(localStorage.objanswer || "{}"), //储存上传失败的问题答案
		Popout:false, //评估评价弹窗
		evaluate:{},//评价信息数据
		ip:localStorage.ip,
	},
	methods: {
		listtoggle: function(n) {
			this.isActive = n;
			if(n == 3) {
				this.daysclick = new Date().toLocaleDateString();
				this.arrDate.length = 0;
				schedule(new Date().getFullYear(), new Date().getMonth() + 1);
				workList(new Date(), "1");
			} else if(n == 2) {
				this.daysclick = new Date().toLocaleDateString()
				schedule(new Date().getFullYear(), new Date().getMonth() + 1);
				workList(new Date(), "0");
			} else if(n == 1) {
				taday();
			}
			if(n != 1) this.initData(null);
		},
		// 返回 类似 2016-01-02 格式的字符串
		formatDate: function(year, month, day) {
			var y = year;
			var m = month;
			if(m < 10) m = "0" + m;
			var d = day;
			if(d < 10) d = "0" + d;
			return y + "-" + m + "-" + d
		},
		isVerDate(v) { //includes() 方法用来判断一个数组是否包含一个指定的值，如果是，酌情返回 true或 false。
			var v_day = this.formatDate(v.getFullYear(), v.getMonth() + 1, v.getDate())
			return(this.arrDate.indexOf(v_day) >= 0)
		},
		initData: function(cur) { 
			var date;
			var now = new Date();
			var nowday = new Date(this.formatDate(now.getFullYear(), now.getMonth() + 1, now.getDate()))
			var daysarr = [];
			if(cur) {
				date = new Date(cur);
			} else {
				if(now.getMonth() == 0) {
					var d = new Date(this.formatDate(now.getFullYear() - 1, 12, 1));
				} else {
					var d = new Date(this.formatDate(now.getFullYear(), now.getMonth(), 1));
				}
				d.setDate(42);
				date = new Date(this.formatDate(d.getFullYear(), d.getMonth() + 1, 1));
			}

			this.currentDay = date.getDate();
			this.currentYear = date.getFullYear();
			this.currentMonth = date.getMonth() + 1;
			this.currentWeek = date.getDay();
			var str = this.formatDate(this.currentYear, this.currentMonth, this.currentDay);
			daysarr.length = 0;
			// 今天是周日6，放在第一行第7个位置，前面6个
			//初始化本周
			for(var i = this.currentWeek; i >= 0; i--) {
				var d = new Date(str);
				d.setDate(d.getDate() - i);
				var dayobject = {
					day: d,
					isSign: this.isVerDate(d),
					colorC: d > now,
					today: d.getTime() == nowday.getTime(),
				}
				daysarr.push(dayobject); //将日期放入data 中的days数组 供页面渲染使用
			}
			//其他周
			for(var i = 1; i < 42 - this.currentWeek; i++) {
				var d = new Date(str);
				d.setDate(d.getDate() + i);
				var dayobject = {
					day: d,
					isSign: this.isVerDate(d),
					colorC: d > now,
					today: d.getTime() == nowday.getTime()
				}
				daysarr.push(dayobject);
			}
			this.days = daysarr
		},
		pickPre: function(year, month) {
			this.daysclick = null;
			var d = new Date(this.formatDate(year, month, 1));
			d.setDate(0);
			this.initData(this.formatDate(d.getFullYear(), d.getMonth() + 1, 1));
			schedule(d.getFullYear(), d.getMonth() + 1, true)
		},
		pickNext: function(year, month) {
			this.daysclick = null;
			var d = new Date(this.formatDate(year, month, 1));
			d.setDate(42);
			this.initData(this.formatDate(d.getFullYear(), d.getMonth() + 1, 1));
			schedule(d.getFullYear(), d.getMonth() + 1, true)
		},
		clickevent: function(daynum, day) {
			this.daysclick = day.toLocaleDateString();
			if(this.isActive == 2) {
				workList(day, "0")
			} else {
				workList(day, "1")
			}
		},
		openurl: function(numb) {
			if(this.daysclick != new Date().toLocaleDateString()) {
				log_message('预约时间与当前时间不符')
				return false;
			}
			sessionStorage.info_progect = JSON.stringify(numb);
			window.location.href = 'lnto_project.html';
		},
		offlineajax: function(index) {
			console.log(index)
			submitanswer(this.objanswer, index)
		}
	}

})
var response = null;
//初始化地图对象，加载地图
var map = new AMap.Map("container", {
	resizeEnable: true,
	center: [121.39, 37.52], //地图中心点
	zoom: 13 //地图显示的缩放级别
});
//添加点标记，并使用自己的icon
var markers = [];

function addmap(longitude, latitude, mapIcpn) {
	var marker = new AMap.Marker({
		map: map,
		position: [longitude, latitude],
		icon: new AMap.Icon({
			size: new AMap.Size(65, 95), //图标大小
			image: mapIcpn,
			imageOffset: new AMap.Pixel(0, 0)
		})
	});
	markers.push(marker);
}

function mapinit() {
	for(var i = 0; i < response.length; i++) {
		if(i == 0) {
			if(response[i].longitude && response[i].latitude) {
				map.setZoomAndCenter(13, [response[i].longitude, response[i].latitude]);
				addmap(response[i].longitude, response[i].latitude, "../../img/pinggu/03.png")
			}
		} else {
			if(response[i].longitude && response[i].latitude) {
				addmap(response[i].longitude, response[i].latitude, "../../img/pinggu/04.png")
			}
		}
	}
}

document.querySelector('.mui-slider').addEventListener('slide', function(event) {
	map.remove(markers);
	markers.length = 0;
	for(var i = 0; i < response.length; i++) {
		if(i == event.detail.slideNumber) {
			if(response[i].longitude && response[i].latitude) {
				map.panTo([response[i].longitude, response[i].latitude]);
				addmap(response[i].longitude, response[i].latitude, "https://tiankongoyll.github.io/img/03.png")
			}
		} else {
			if(response[i].longitude && response[i].latitude) {
				addmap(response[i].longitude, response[i].latitude, "https://tiankongoyll.github.io/img/04.png")
			}
		}
	}
});

function taday() {
	ajaxGet(
		Url.ASSESS_TODAY, {
			"nub": parseInt(Math.random() * 100000)
		},
		function(data) {
			if(data.status == 1) {
				main.response = data.response;
				response = data.response;
				mapinit();
			}
			document.getElementById("main").setAttribute("style", "display: block;");
		}
	)
}
taday();

function schedule(year, mouth, num) {
	var type = "1";
	if(main.isActive == '2'){
		type = "0";
	}
	ajaxGet(
		Url.ASSESS_SCHEDULE, {
			"date": year + "-" + mouth,
			"type": type
		},
		function(data) {
			var notfinishday = [];
			if(data.status == 1) {
				main.arrDate.length = 0;
				for(var i = 0; i < data.response.length; i++) {
					notfinishday.push(data.response[i].notFinishDay)
				}
			}
			main.arrDate = notfinishday;
			//			main.arrDate=notfinishday;
			if(num) {
				main.initData(main.formatDate(year, mouth, 1))
			} else {
				main.initData(null);
			}
		}
	)
}

function workList(date, type) {
	var timedate = date.getFullYear() + "/" + (date.getMonth() + 1) + "/" + date.getDate();
	ajaxGet(
		Url.ASSESS_WORKLIST, {
			"date": timedate,
			"type": type
		},
		function(data) {
			if(data.status == 1) {
				main.response2 = data.response;
			}
		}
	)
}
//提交选题答案
function submitanswer(objanswer, index) {
	ajaxPostJson(
		Url.ASSESS_SAVASSESS,
		objanswer,
		function(data) {
			if(data.status == 1) {
				main.response2.splice(index, 1);
			} else {
				log_message(data.message)
			}
		}
	)
}